<template>
	<view>
		<view class="user-info">
			<u-avatar :src="nickImg"></u-avatar>
			<view class="username">
				<u-button :disabled="disabled" v-if="!isLogin" @click="login" type="info" size="medium">点击登录</u-button>
				<view v-else class="">
					<view><text> 欢迎您，{{nickname}}</text></view>
				</view>
			</view>
		</view>

		<view class="">
			<u-cell-group>
				<navigator url="./editUserinfo/editUserinfo" hover-class="navigator-hover">
					<u-cell-item icon="setting-fill" title="编辑个人信息"></u-cell-item>
				</navigator>

				<navigator url="./userlist/userlist" hover-class="navigator-hover">
					<u-cell-item icon="account-fill" title="查看好友列表"></u-cell-item>
				</navigator>

				<navigator url="./userhome/userhome" hover-class="navigator-hover">
					<u-cell-item icon="home-fill" title="个人主页" value="新版本"></u-cell-item>
				</navigator>

			</u-cell-group>



		</view>


	</view>
</template>

<script>
	export default {
		data() {
			return {
				nickImg: 'https://img0.baidu.com/it/u=1429435380,946942033&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
				nickname: '',
				openid: '',
				disabled: true,
				userinfo: {},
				isLogin: false,
			}
		},

		onReady() {
			var that = this;
			uni.login({
				success(res) {
					uniCloud.callFunction({
						name: 'adduser',
						data: {
							code: res.code
						},
					}).then(data => {
						const db = uniCloud.database()
						db.collection('users').where({
							openid: data.result.openid
						}).get().then(res => {
							if (res.result.data.length) {
								getApp().globalData.user = Object.assign(getApp().globalData.user,
									res.result.data[0])
								that.nickImg = getApp().globalData.user.imgSrc;
								that.nickname = getApp().globalData.user.nickname;
								that.isLogin = true
								that.getUsermessage()
							} else {
								that.disabled = false
							}
						})
					})
				}
			})
		},


		onShow() {
			this.nickImg = getApp().globalData.user.imgSrc;
			this.nickname = getApp().globalData.user.nickname;
		},


		methods: {
			login() {
				var that = this
				uni.getUserProfile({
					desc: "获取你的昵称、头像、地区及性别",
					success: (res) => {
						that.userinfo = res.userInfo
						uni.login({
							provider: 'weixin',
							success(res) {

								uniCloud.callFunction({
									name: 'adduser',
									data: {
										code: res.code
									},
								}).then(data => {
									//这里就是把返回值赋值后，
									that.openid = data.result.openid
									
									that.addUser()
									
								}).catch(err => {
									console.log(err)
								})


							},
							fail(err) {

							}
						})
					},
					fail() {

					},
				})
			},


			addUser() { 
				let user = {
					"imgSrc": this.userinfo.avatarUrl,
					"nickname": this.userinfo.nickName,
					"openid": this.openid,
					"signature": "",
					"phoneNumber": "",
					"wxNumber": "",
					"isLoc": false,
					"likes": 0,
					"time": new Date(),
					"gender": ""
				};
				const db = uniCloud.database()
				db.collection('users').add(user).then(res => {
					db.collection('users').doc(res.result.id).get().then(data => {
						getApp().globalData.user = Object.assign(getApp().globalData.user, data.result.data[0])
						this.nickImg = getApp().globalData.user.imgSrc;
						this.nickname = getApp().globalData.user.nickname;
						this.isLogin = true
						
					})
				}).catch(err => {
					console.log(err)
				})
				
			},


          getUsermessage(){
			const db = uniCloud.database()
			db.collection('message').where({
				userId:getApp().globalData.user._id
			}).get().then(res=>{
				if (res.result.data[0].list.length) {
					getApp().globalData.message = res.result.data[0].list
					uni.showTabBarRedDot({
						index:2
					})
				} else{
					
				}
			}).catch(err=>{
				return
			})
		  },



		}
	}
</script>

<style>
	.user-info {
		width: 100%;
		height: 200rpx;
		/* background-color: red; */

		display: flex;
		border-bottom: 10rpx #DCDFE6 solid;
		align-items: center;
	}

	.username {
		font-size: 30rpx;
		margin-left: 30rpx;
	}
</style>
